/**
 * Copyright ©2012-2018 HopeHaiyang Corporation, All Rights Reserved
 * http://www.wuhanhope.com
 */
package hope.platform.data.web;

import hope.platform.data.domain.Emergency;
import hope.platform.data.server.data.header.EmergencyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * class info
 *
 * @author hujia
 *         Create at 2018/7/25 11:15
 */
@RestController
@RequestMapping("/emergency")
public class EmergencyController {

    @Autowired
    private EmergencyService service;

    @RequestMapping(method = RequestMethod.POST)
    public Emergency addEmergency(@RequestBody Emergency emergency) throws Exception {
        return service.addEmergency(emergency);
    }

    @RequestMapping(method = RequestMethod.PUT)
    public Emergency updateEmergency(@RequestBody Emergency emergency) throws Exception {
        return service.updateEmergency(emergency);
    }

    @RequestMapping(value = "/all/{pageNumber}/{pageSize}/{sortType}", method = RequestMethod.GET)
    public Page<Emergency> getAll(@PathVariable int pageNumber,
                                  @PathVariable int pageSize,
                                  @PathVariable String sortType) throws Exception {
        return service.findAll(PageRequest.of(pageNumber, pageSize, Sort.Direction.DESC, sortType));
    }

    @RequestMapping(value = "/userName/{userName}/pageNumber/{pageNumber}/pageSize/{pageSize}/sortType/{sortType}")
    public Page<Emergency> getByUserName(@PathVariable String userName,
                                         @PathVariable int pageNumber,
                                         @PathVariable int pageSize,
                                         @PathVariable String sortType) throws Exception {
        return service.findByUserName(userName, PageRequest.of(pageNumber, pageSize, Sort.Direction.DESC, sortType));
    }
}
